package com.nebula.salary.portal.controller;

import com.google.common.collect.Maps;
import com.nebula.salary.common.constants.ReturnMessage;
import com.nebula.salary.common.result.ServerResponse;
import com.nebula.salary.common.utils.SecurityUtil;
import com.nebula.salary.model.dto.WorkOvertimeDto;
import com.nebula.salary.model.pojo.WorkOvertime;
import com.nebula.salary.model.vo.PageResult;
import com.nebula.salary.portal.service.IWorkOvertimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * <p>
 * 加班记录表 前端控制器
 * </p>
 *
 * @author codex
 * @since 2022-06-09
 */
@RestController
@RequestMapping("/workOvertime/record")
@Api(tags = "加班记录管理")
@Transactional(rollbackFor = Exception.class)
public class WorkOvertimeController {

    @Autowired
    private IWorkOvertimeService workOvertimeService;

    @ApiOperation("删除加班记录")
    @DeleteMapping("/{recordIds}")
    public ServerResponse<String> delete(
            @ApiParam(value = "要删除的加班记录ID数组", required = true)
            @PathVariable Integer[] recordIds) {
        return workOvertimeService.deleteRecords(recordIds);
    }

    @ApiOperation("修改加班记录")
    @PutMapping
    public ServerResponse<String> update(@RequestBody WorkOvertime workOvertime) {
        if (workOvertime.getId() == null || workOvertime.getId() <= 0) {
            return ServerResponse.createByErrorMessage("参数错误，记录代号有误");
        }

        workOvertimeService.checkData(workOvertime);

        workOvertimeService.checkDataScope(new Integer[]{workOvertime.getId()});

        workOvertime.setYear(workOvertime.getStartTime().getYear());

        return workOvertimeService.updateById(workOvertime)
                ? ServerResponse.createBySuccessMessage(ReturnMessage.UPDATE_SUCCESS)
                : ServerResponse.createByErrorMessage(ReturnMessage.UPDATE_ERROR);
    }

    @ApiOperation("加班打卡")
    @PostMapping
    public ServerResponse<String> clock(@RequestBody WorkOvertime workOvertime) {
        return workOvertimeService.clock(workOvertime) ? ServerResponse.createBySuccessMessage("打卡成功")
                : ServerResponse.createByErrorMessage("打卡失败");
    }

    @ApiOperation("根据代号获取数据")
    @GetMapping("/{recordId}")
    public ServerResponse<WorkOvertime> getById(@PathVariable
                                                @ApiParam("记录代号") Integer recordId) {

        workOvertimeService.checkDataScope(new Integer[]{recordId});

        return ServerResponse.createBySuccess(workOvertimeService.getById(recordId));
    }

    @ApiOperation("根据时间范围查询加班记录")
    @GetMapping("/")
    public ServerResponse<Map<String, Object>> getRecord(WorkOvertimeDto workOvertime) {

        // 判断时间是否为空
        if (workOvertime.getBeginTime() == null) {
            return ServerResponse.createByErrorMessage("开始时间不能为空");
        }

        if (workOvertime.getEndTime() == null) {
            return ServerResponse.createByErrorMessage("结束时间不能为空");
        }

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        if (LocalDate.parse(workOvertime.getBeginTime(), df)
                .isAfter(LocalDate.parse(workOvertime.getEndTime(), df))) {
            return ServerResponse.createByErrorMessage("开始时间不能大于结束时间");
        }

        PageResult pageResult = workOvertimeService.getRecord(workOvertime);

        BigDecimal overtimeDays = workOvertimeService.getTotalWorkOvertimeDays(workOvertime);

        Map<String, Object> map = Maps.newHashMap();
        map.put("pageResult", pageResult);
        map.put("overtimeDays", overtimeDays);

        return ServerResponse.createBySuccess(map);
    }
}

